import Vue from 'vue'
import Vuex from 'vuex'
import musicApi from './request/musicApi'

Vue.use(Vuex);


export default new Vuex.Store({
    state: {
        currentPlayTime: 0,
        currentSongId: '',
        currentMusicUrl: '',
        currentMusicName: '',
        currentMusicArtist: '',
        currentMusicDetail: '',
        currentMusicCoverUrl: '',
        currentMusicIndex: '',
        currentMusicMv: 0,
        isPlay: false,
        duration: 0,
        playMode: ['single', 'order', 'shuffle'],//single,order,shuffle
        playList: [],
        homeImageCounter:'',//首页image总数,初始值为‘’防止初始值为0满足show条件
        homeImageLoadedCounter:0
    },
    mutations: {
        setPlayStatus(state, newV) {
            state.isPlay = newV;
        }, setCurrentPlayTime(state, newV) {
            state.currentPlayTime = newV;
        }, setDuration(state, v) {
            state.duration = v;
        }, setCurrentSongId(state, newV) {
            state.currentSongId = newV;
        }, setCurrentMusicUrl(state, newV) {
            state.currentMusicUrl = newV;
        }, setCurrentMusicName(state, newV) {
            state.currentMusicName = newV;
        }, setCurrentMusicArtist(state, newV) {
            state.currentMusicArtist = newV;
        }, setCurrentMusicDetail(state, newV) {
            state.currentMusicDetail = newV;
        }, setCurrentMusicCoverUrl(state, newV) {
            state.currentMusicCoverUrl = newV;
        }, setCurrentMusicMv(state, newV) {
            state.currentMusicMv = newV;
        }, setMusicPlayList(state, newV) {
            state.playList = newV;
        }, addMusicToPlayList(state, newV) {
            state.playList.push(newV);
            localStorage.setItem('playList', JSON.stringify(state.playList))
        }, removeMusicFromPlayList(state, index) {
            if (index == state.currentMusicIndex) {
                if (index == state.playList.length - 1 && state.playList.length != 1) {//不需要考虑index==0，因为0的时候删除按钮不会触发
                    localStorage.setItem('latPlayMusicIndex', state.playList.length - 2);
                    state.currentMusicIndex = state.playList.length - 2;
                }
                else if (index == state.playList.length - 1 && state.playList.length == 1) {
                    localStorage.removeItem('latPlayMusicIndex');
                    state.currentMusicIndex = '';
                }
                else {
                    localStorage.setItem('latPlayMusicIndex', index);
                    state.currentMusicIndex = index;
                }
                state.playList.splice(index, 1);
            } else {
                state.playList.splice(index, 1);
                for (let i = 0; i < state.playList.length; i++) {
                    if (state.currentSongId == state.playList[i].id) {
                        state.currentMusicIndex = i;
                        break;
                    }
                }
            }
            localStorage.setItem('playList', JSON.stringify(state.playList));
            if (state.playList.length == 0) {
                localStorage.removeItem('latPlayMusicIndex');
            } else {
                state.currentMusicArtist = state.playList[state.currentMusicIndex].artist;
                state.currentMusicMv = state.playList[state.currentMusicIndex].mv;
                state.currentMusicName = state.playList[state.currentMusicIndex].name;
                state.currentMusicCoverUrl = state.playList[state.currentMusicIndex].picUrl;
                state.currentSongId = state.playList[state.currentMusicIndex].id;
            }
            console.log(state.playList)
        }, removeAllFromPlayList(state) {
            state.playList = [];
        }, setCurrentMusicIndex(state, index) {
            state.currentMusicIndex = index;
        }, clearInformation(state) {
            state.currentPlayTime = 0;
            state.currentSongId = '';
            state.currentMusicUrl = '';
            state.currentMusicName = '';
            state.currentMusicArtist = '';
            state.currentMusicDetail = '';
            state.currentMusicCoverUrl = '';
            state.currentMusicIndex = '';
            state.currentMusicMv = 0;
            state.isPlay = false;
            state.duration = 0;
            state.playList = [];
        },
        addCounter(state){
            if(state.homeImageCounter=='')
                state.homeImageCounter=0
            state.homeImageCounter++;
        },
        addLoadedCounter(state){
            state.homeImageLoadedCounter++;
        },
        checkLoaded(state){
            return state.homeImageLoadedCounter==state.homeImageCounter;
        }
    },
    actions: {
        // getAsyncMusicUrl({commit, state}) {
        //     return musicApi.getMusicUrl({id: state.currentSongId});
        // },
    }
});
